home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ARASAN_S.ZIP / BOOKWRIT.CPP < prev    next >
C/C++ Source or Header  |  1993-11-17  |  2KB  |  62 lines

  1. // Copyright 1992 by Jon Dart.  All Rights Reserved.
  2.  
  3. #include "bookwrit.h"
  4. #include "constant.h"
  5. #include <assert.h>
  6. #include <mem.h>
  7.  
  8. const int Entry_Size = sizeof(Book_Entry);
  9. const int Header_Size = sizeof(unsigned long) + 1;
  10. const int Buffer_Size = 4096;
  11. // Setting this to the book size will keep the whole book in memory,
  12. // which will improve speed.
  13. const char Book_File_Name[] = "book";
  14.  
  15. static byte *write_buffer;
  16.  
  17. Book_Writer::Book_Writer( const byte version, const unsigned size )
  18. : book_file( Book_File_Name, 
  19.          ios::out | ios::trunc | ios::binary ),
  20.   my_size(size),
  21.   my_version(version),
  22.   index(0)
  23. {
  24.    is_open = False;
  25.    if (!book_file.good())
  26.       return;
  27.    is_open = True;
  28.    book_file.write( (char*)&my_version, 1 );
  29.    book_file.write( (char*)&my_size, (int)sizeof(unsigned));
  30.    write_buffer = new byte[Buffer_Size];
  31. }
  32.  
  33. Book_Writer::~Book_Writer()
  34. {
  35.    if (index)
  36.       book_file.write( write_buffer, index );
  37.    book_file.close();
  38.    delete [] write_buffer;
  39. }
  40.  
  41. Boolean Book_Writer::Write( Book_Entry &be )
  42. {
  43.    assert(is_open);
  44.  
  45.    if (index + sizeof(be) <= Buffer_Size )
  46.    {
  47.       memcpy(write_buffer+index,&be,sizeof(be));
  48.       index += sizeof(be);
  49.    }
  50.    else
  51.    {
  52.       unsigned to_go = Buffer_Size - index;
  53.       memcpy(write_buffer+index,&be,to_go);
  54.       book_file.write( write_buffer, Buffer_Size );
  55.       memcpy(write_buffer,&be + to_go,sizeof(be)-to_go);
  56.       index = sizeof(be)-to_go;
  57.    }
  58.    return True;
  59. }
  60.  
  61.  
  62.